package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version V1.0
 * @author: sujiahao
 * @date: 2020/2/12 17:45
 * @description:
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenu")
    public Result findMenu(){
        List<Menu> menu = menuService.findMenu();
        return new Result(true, "查询成功",menu);
    }

    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        Menu menu = menuService.queryById(id);
        return new Result(true, "查询成功",menu);
    }

    @RequestMapping("/updataById")
    public Result updataById(@RequestBody Menu menu){
       menuService.updataById(menu);
        return new Result(true, "修改成功");
    }

    /**
     * 添加菜单
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
      try {
          menuService.add(menu);
          return new Result(true, "添加成功");
      }catch (Exception e){
          e.printStackTrace();
          return new Result(false, "添加失败");
      }
    }

    /**
     * 删除菜单
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
      try {
          System.out.println(id);
          menuService.deleteById(id);
          return new Result(true, "删除成功");
      }catch (Exception e){
          e.printStackTrace();
          return new Result(false, "删除失败");
      }
    }



    @RequestMapping("/getdata")
    public Result getData(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return menuService.findByRoleId(user.getUsername());
    }

}
